
$(document).ready(function() {
	var markers = new Array();
	
    var myOptions = {
  	      mapTypeId: google.maps.MapTypeId.ROADMAP,
  	    };
  	var map = null;
  	
  	function refreshMarkers(newMarkers,mapDefault) {
  		for (var i=0; i < markers.length; i++) {
  			markers[i].setMap(null);
  		}
  		markers = new Array();
  		for (var i=0; i < newMarkers.length; i++) {
  			appendMarker(newMarkers[i]);
  		}
  		if (markers.length > 0) {
  			fitMap();
  		} else {
  			map.setCenter(new google.maps.LatLng(mapDefault.b,mapDefault.c));
  			map.setZoom(mapDefault.zoom);
  		}
  	}
  	
	function appendMarker(markerObj) {
		//console.log(markerObj);
		var latLng = new google.maps.LatLng(markerObj.b,markerObj.c);
        var marker = new google.maps.Marker({position:latLng,map:map});
        markers.push(marker);
        google.maps.event.addListener(marker, 'mouseup', function(event) {
            console.log('click');
        });    	 	
	}
	
    function fitMap() {
    	var bounds = new google.maps.LatLngBounds();
    	for (var i = 0; i < markers.length; i++) {
    	    bounds.extend(markers[i].position);
    	}
    	
    	map.fitBounds(bounds);
    	boundsChangedListener = google.maps.event.addListener(map, 
    			'bounds_changed', function() { 
		        	if (map.getZoom() > 14)
		        		map.setZoom(14);
    			}
    	);    	

    }

	var options = {
		urlData : location.pathname + 'getPoiResultsPage/',
		fnRequest : function(url, data, onSuccess) {
			//console.log('request: ' + url);
			//console.log('json: ' + postData.json);
			$.ajax({
				type: 'POST',
				url: url, 
				data : data.filter,
				dataType: "json",
				success: function(response, textStatus) {
					if (onSuccess) {
						if (!map) {
							var latlng = new google.maps.LatLng(response.mapDefault.b,response.mapDefault.c);
							myOptions.center = latlng;
													
							map = new google.maps.Map(document.getElementById("OverviewMap"), myOptions);
						}
						onSuccess(response);
						refreshMarkers(response.markers,response.mapDefault);
						
					}
				}
			});
		}
	}
	
    $('#PoiOverview').dataManager(options);
});